from fastapi import APIRouter, Depends
from crud.systems import user as user_crud
from crud.users import users as users_crud
from common import response_code
import models
from dependencies import engine, SessionDep
from typing import Annotated
from routers.auth import (
    get_current_sys_user,
    get_current_patient_user,
    get_current_doctor_user,
)


router = APIRouter()


"""查询全部用户（医生/患者）

Keyword arguments:
skip -- 起始页 (default 0)
limit -- 每页条数 (default 20)
username -- 用户名 (default None)
name -- 姓名 (default None)
phone -- 手机号 (default None)
type -- 用户类型 (default None)
Return: response -- 用户列表数据
"""


@router.get(
    "/api/users/",
    tags=["用户管理"],
    summary="查询全部用户（医生/患者）",
    response_model=models.RespJsonRegisterUser,
)
def get_user(
    skip: int = 0,
    limit: int = 20,
    username: str | None = None,
    phone: str | None = None,
    type: int | None = None,
    user: Annotated[models.SystemUser, Depends(get_current_sys_user)] = None,
    response: models.RespJsonRegisterUser = Depends(
        response_code.RespSuccessJson.resp_200
    ),
):
    # 查询用户列表
    items = users_crud.get_user(skip, limit, username, phone, type)
    # 赋值给响应结果
    response.data = items
    return response


"""新增医生

Keyword arguments:
doctor_create -- 新增医生信息
    username -- 用户名
    phone -- 手机号
    docotr -- 医生信息
        name -- 医生姓名
        job_num -- 工号
        phone -- 手机号
        title -- 职称
        department -- 科室
        org_id -- 所属机构
        is_active -- 状态
Return: response -- 新增医生
"""


@router.post(
    "/api/users/doctor/",
    tags=["用户管理"],
    summary="新增医生",
    response_model=models.RespJsonRegisterUser,
)
def create_doctor(
    doctor_create: models.UserDoctorCreate,
    user: Annotated[models.SystemUser, Depends(get_current_sys_user)],
    response: models.RespJsonRegisterUser = Depends(
        response_code.RespSuccessJson.resp_201
    ),
):
    # 新增用户
    users_crud.create_doctor(doctor_create, user)

    return response


"""编辑医生

Keyword arguments:
id -- 用户（医生）id
doctor_create -- 新增医生信息
    username -- 用户名
    phone -- 手机号
    docotr -- 医生信息
        name -- 医生姓名
        job_num -- 工号
        phone -- 手机号
        title -- 职称
        department -- 科室
        org_id -- 所属机构
        is_active -- 状态
Return: response -- 编辑医生
"""


@router.put(
    "/api/users/doctor/{id}",
    tags=["用户管理", "医生端"],
    summary="编辑医生",
    response_model=models.RespJsonRegisterUser,
)
def update_doctor(
    id: int,
    doctor_update: models.UserDoctorUpdate,
    user: Annotated[models.SystemUser, Depends(get_current_sys_user)],
    response: models.RespJsonRegisterUser = Depends(
        response_code.RespSuccessJson.resp_202
    ),
):
    # 编辑用户
    users_crud.update_doctor(id, doctor_update, user)
    return response


"""患者注册

Keyword arguments:
patient_create -- 新增患者信息
    username -- 用户名
    phone -- 手机号
    password -- 密码
Return: 
response -- 患者注册
"""


@router.post(
    "/api/users/patient/login",
    tags=["患者端"],
    summary="患者注册",
    response_model=models.RespJsonRegisterUser,
)
def create_patient(
    patient_create: models.UserPatientsCreate,
    response: models.RespJsonRegisterUser = Depends(
        response_code.RespSuccessJson.resp_201
    ),
):
    # 新增用户
    users_crud.create_patient(patient_create)

    # 修改提示语
    response.msg = "注册成功"
    return response


"""患者手机号变更

Keyword arguments:
id -- 用户id
old_phone -- 旧手机号
new_phone -- 新手机号
Return: 
response -- 变更手机号
"""


@router.put(
    "/api/users/patient/phone/modify/{id}",
    tags=["患者端"],
    summary="变更手机号",
    response_model=models.RespJsonRegisterUser,
)
def update_patient(
    id: int,
    old_phone: str,
    new_phone: str,
    user: Annotated[models.User, Depends(get_current_patient_user)],
    response: models.RespJsonRegisterUser = Depends(
        response_code.RespSuccessJson.resp_202
    ),
):
    # 变更手机号
    users_crud.modify_phone(id, old_phone, new_phone)

    # 修改提示语
    response.msg = "手机号变更成功"
    return response


"""修改密码
Keyword arguments:
id -- 用户id
old_password -- 旧密码
new_password -- 新密码
Return: 
response -- 修改密码
"""


@router.put(
    "/api/users/password/modify/{id}",
    tags=["用户管理"],
    summary="修改密码",
    response_model=models.RespJsonRegisterUser,
)
def modify_password(
    id: int,
    old_password: str,
    new_password: str,
    user: Annotated[models.SystemUser, Depends(get_current_sys_user)],
    response: models.RespJsonRegisterUser = Depends(
        response_code.RespSuccessJson.resp_202
    ),
):
    # 修改密码
    users_crud.modify_password(id, old_password, new_password, user)

    response.msg = "密码修改成功"
    return response


"""重置密码
Keyword arguments:
id -- 用户id
Return: 
response -- 重置密码
"""


@router.put(
    "/api/users/password/reset/{id}",
    tags=["用户管理"],
    summary="重置密码",
    response_model=models.RespJsonRegisterUser,
)
def reset_password(
    id: int,
    user: Annotated[models.SystemUser, Depends(get_current_sys_user)],
    response: models.RespJsonRegisterUser = Depends(
        response_code.RespSuccessJson.resp_202
    ),
):
    # 重置密码
    users_crud.reset_password(id, user)

    response.msg = "密码重置成功"
    return response


"""删除用户（医生/患者）

Keyword arguments:
id -- 用户id
Return: response -- 删除用户
"""


@router.delete(
    "/api/users/{id}",
    tags=["用户管理"],
    summary="删除用户（医生/患者）",
    response_model=models.RespJsonRegisterUser,
)
def delete_user(
    id: int,
    user: Annotated[models.SystemUser, Depends(get_current_sys_user)],
    response: models.RespJsonRegisterUser = Depends(
        response_code.RespSuccessJson.resp_204
    ),
):
    # 删除用户（医生/患者）
    users_crud.delete_user(id)
    return response


"""添加就诊人（患者）

Keyword arguments:
add_patient -- 添加就诊人信息
    name -- 姓名
    sex -- 性别
    birthday  -- 生日
    id_card -- 身份证号
    outpatient_num  -- 门诊号
    inpatient_num   -- 住院号
    medical_record_num   -- 病历号
    phone   -- 手机号
user -- 用户信息（患者/医生）
Return: 
add_patient -- 添加就诊人
"""


@router.post(
    "/api/patient/add",
    tags=["患者端"],
    summary="添加就诊人",
    response_model=models.RespJsonPatients,
)
def add_patient(
    add_patient: models.PatientCreate,
    user: Annotated[models.User, Depends(get_current_patient_user)],
    response: models.RespJsonPatients = Depends(response_code.RespSuccessJson.resp_201),
):
    # 添加就诊人
    users_crud.add_patient(add_patient, user)
    return response


"""关联就诊人（患者）

Keyword arguments:
id_card -- 就诊人身份证号
phone -- 就诊人手机号
Return: 
add_patient -- 关联就诊人
"""


@router.post(
    "/api/patient/relate",
    tags=["患者端"],
    summary="关联就诊人",
    response_model=models.RespJsonPatients,
)
def relate_patient(
    id_card: str,
    phone: str,
    user: Annotated[models.User, Depends(get_current_patient_user)],
    response: models.RespJsonPatients = Depends(response_code.RespSuccessJson.resp_201),
):
    # 关联就诊人
    users_crud.relate_patient(id_card, phone, user)
    response.msg = "关联成功"
    return response
